﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using System;
using ServerPublic.Data;
using ServerGame.GameObject;
using static UtilPublic.Serialization.Jsh;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;
using System.Linq;

namespace ServerGame.Manager
{
    /// <summary>
    /// 管理 房间
    /// </summary>
    public static class RoomMgr
    {
        public static bool RoomCreate(this ConfigRoomExt confext, out Room room)
        {
            var conf = confext.SettBase;
            room = new Room();
            if (!DataGame.GetCity(conf.City, out var city))
            {
                return false;
            }
            room.Type = EMudObjType.Room;
            room.CityCoor = city.CityCoor;
            room.RoomCoor = conf.RoomX * 100 + conf.RoomY;
            room.Uid = room.CityCoor * 1_0000 + room.RoomCoor;
            room.CfgExt = confext;
            room.SetExits();

            room.RoomUpdate();
            return true;
        }
        public static string OppExit(this string exit)
        {
            switch (exit)
            {
                case "东": return "西";
                case "南": return "北";
                case "西": return "东";
                case "北": return "南";
                case "东南": return "西北";
                case "东北": return "西南";
                case "西北": return "东南";
                case "西南": return "东北";
                default: return "中";
            }
        }
        public static void SetExits(this Room room)
        {
            foreach (var text in room.Cfg.Exits)
            {
                if (GetExit(room, text, out var exit))
                {
                    room.DictExit[exit.Dir] = exit;
                }
            }
        }
        public static bool GetExit(Room room, string text, out RoomExit exit)
        {
            exit = new RoomExit()
            {
                Dir = text,
            };
            switch (exit.Dir)
            {
                case "东":
                    exit.RoomUid = room.Uid + 100;
                    break;
                case "西":
                    exit.RoomUid = room.Uid - 100;
                    break;
                case "南":
                    exit.RoomUid = room.Uid - 1;
                    break;
                case "北":
                    exit.RoomUid = room.Uid + 1;
                    break;
                case "东南":
                    exit.RoomUid = room.Uid + 100 - 1;
                    break;
                case "西南":
                    exit.RoomUid = room.Uid - 100 - 1;
                    break;
                case "东北":
                    exit.RoomUid = room.Uid + 100 + 1;
                    break;
                case "西北":
                    exit.RoomUid = room.Uid - 100 + 1;
                    break;
                default:
                    break;
            }
            return true;
        }
        public static void RoomUpdate()
        {
            loger.Warn($"更新所有房间。。。");
            foreach (var room in DataGame.DictRoom.Values)
            {
                RoomUpdate(room);
            }
            loger.Warn($"更新所有房间。。。完成");
        }
        public static void RoomUpdate(this Room room)
        {
            var confext = room.CfgExt;

            foreach (var kvp in confext.DictItems)
            {
                if (ItemMgr.ItemCreate(kvp.Key, kvp.Value, room, out var item))
                {
                    room.DictItem[item.ConfigId] = item;
                }
            }
            foreach (var kvp in confext.DictEquips)
            {
                var oldcount = room.DictEquip.Values.Count(it => it.ConfigId == kvp.Key);
                if (oldcount < kvp.Value)
                {
                    for (int i = 0; i < (kvp.Value - oldcount); i++)
                    {
                        if (EquipMgr.EquipCreate(kvp.Key, room, out var equip))
                        {
                            room.DictEquip[equip.Uid] = equip;
                        }
                    }
                }
            }
            foreach (var kvp in confext.DictNpcs)
            {
                var oldcount = room.DictNpc.Values.Count(it => it.ConfigId == kvp.Key);
                if (oldcount < kvp.Value)
                {
                    for (int i = 0; i < (kvp.Value - oldcount); i++)
                    {
                        if (NpcMgr.NpcCreate(kvp.Key, room, out var npc))
                        {
                            room.DictNpc[npc.Uid] = npc;
                        }
                    }
                }
            }
        }
        //登录进入
        public static bool RoomEnterLogin(this Player player)
        {
            if (player == null)
            {
                return false;
            }
            //一般初始化
            if (!DataGame.GetRoom(player.DaCommon.RoomUid, out var room))
            {
                room = DataGame.BaseRoom;
            }
            else
            {
                player.Room = DataGame.BaseRoom;
                player.DaCommon.RoomUid = player.Room.Uid;
            }

            room.RoomIn(player);

            player.Room.RoomMessage($"{player.Name}连线进入游戏。", player);

            player.RoomInfo();

            player.RoomInit();

            return true;
        }
        //进入房间
        public static bool RoomGo(this Player player, Room room, string movedir)
        {
            player.Room.RoomOut(player);
            player.Room.RoomMessage($"{player.Name}往{movedir}离开。");

            player.Room.RoomMessage($"{player.Name}从{movedir.OppExit()}面走了过来。");
            room.RoomIn(player);

            player.RoomInfo();

            player.RoomInit();

            return true;
        }
        //离开房间
        public static void RoomOut(this Room room, Player player)
        {
            room.DictPlayer.Remove(player.Puid);
        }
        //进入房间
        public static void RoomIn(this Room room, Player player)
        {
            player.Room = room;
            player.DaCommon.RoomUid = player.Room.Uid;

            player.Room.DictPlayer[player.Puid] = player;
        }
        //进入房间
        public static void RoomInit(this Player player)
        {
            //判断敌人
            player.RoomInitEnemy();
        }

        public static void GetInfo(this Room room, ref CLS_Room_InfoRoom info)
        {
            info.Uid = room.Uid;
            info.ConfigId = room.ConfigId;
            foreach (var item in room.DictExit.Values)
            {
                if (DataGame.GetRoom(item.RoomUid, out var roomexit))
                {
                    var node = new CLS_Room_InfoExit();
                    node.Dir = item.Dir;
                    node.RoomUid = item.RoomUid;
                    node.RoomConfigId = roomexit.ConfigId;
                    info.ListExit.Add(node);
                }
            }
            room.DictItem.ItemInfo(ref info.DictItem);
            room.EquipInfo(ref info.DictEquip);
            foreach (var item in room.DictNpc.Values)
            {
                item.RoomInfoNpc(out var node);
                info.DictNpc[node.Uid] = node;
            }
            foreach (var item in room.DictPlayer.Values)
            {
                item.RoomInfoPlayer(out var node);
                info.DictPlayer[node.Puid] = node;
            }
        }
        public static void RoomInfo(this Player player)
        {
            var Rsp = new G2C_Room_Info();

            player.Room.GetInfo(ref Rsp.RoomInfo);

            player.Send(Rsp);
        }
        public static void RoomThing(this Room room)
        {
            var Rsp = new G2C_Room_RoomThing();

            room.GetInfo(ref Rsp.RoomInfo);

            room.RoomMessage(Rsp);
        }
    }
}
